//
//  SquaredViewController.m
//  Squared
//
//  Created by Cassie Chin on 2/7/14.
//  Copyright (c) 2014 Cassie Chin. All rights reserved.
//

#import "SquaredViewController.h"

@interface SquaredViewController ()

@end

UILabel *colorLabel;
UILabel *scoreLabel;
UILabel *timeLabel;
NSTimeInterval startTime;

NSInteger score;
NSString *currentColor;

const int FPS = 25;
const int SKIP_TICKS = 1000/FPS;

@implementation SquaredViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    /*Initilize all labels*/
    colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,50,300,40)];
    scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,20, 200, 20)];
    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,20, 200, 20)];
    
    /*Initialize and print score*/
    score = 0;
    [self printScore];
    
    /*Initialize and print time*/
    startTime = (double) CACurrentMediaTime();
    [self printTime];
    
    /*Set the next color*/
    [self nextColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)appLoop
{
    
}

-(void)updateLabel:(NSString *)string
{
    colorLabel.numberOfLines = 0;
    colorLabel.backgroundColor = [UIColor clearColor];
    //    colorLabel.textColor = [UIColor whiteColor];
    [self randomTextColor];
    colorLabel.textAlignment = NSTextAlignmentCenter;
    colorLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:32];
    colorLabel.text = string;
    [self.view addSubview:colorLabel];
}
-(void)updateScore:(NSInteger)update
{
    score = score + update;
    [self printScore];
    [self printTime];
}
-(void)printScore
{
    NSString *score_string = [NSString stringWithFormat:@"Score: %d", (int)score];
    scoreLabel.backgroundColor = [UIColor clearColor];
    scoreLabel.textColor = [UIColor whiteColor];
    scoreLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:24];
    scoreLabel.text = score_string;
    [self.view addSubview:scoreLabel];
}
-(void)printTime
{
    CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
    NSString *time_string = [NSString stringWithFormat:@"%lf", elapsedTime];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:24];
    timeLabel.text = time_string;
    [self.view addSubview:timeLabel];
}

-(void)randomTextColor
{
    switch (arc4random() % 4) {
        case 0:
            colorLabel.textColor = [UIColor redColor];
            break;
        case 1:
            colorLabel.textColor = [UIColor blueColor];
            break;
        case 2:
            colorLabel.textColor = [UIColor yellowColor];
            break;
        case 3:
            colorLabel.textColor = [UIColor greenColor];
            break;
        default:
            break;
    }
}
-(void)nextColor
{
    switch (arc4random() % 4)
    {
        case 0:
            [self updateLabel:@"RED"];
            currentColor = @"Red";
            printf("Red\n");
            break;
            
        case 1:
            [self updateLabel:@"BLUE"];
            currentColor = @"Blue";
            printf("Blue\n");
            break;
            
        case 2:
            [self updateLabel:@"YELLOW"];
            currentColor = @"Yellow";
            printf("Yellow\n");
            break;
            
        case 3:
            [self updateLabel:@"GREEN"];
            currentColor = @"Green";
            printf("Green\n");
            break;
            
        default:
            break;
    }
}

- (IBAction)blueButtonPressed
{
    if ([currentColor isEqualToString:@"Blue"])
    {
        [self updateScore:(NSInteger)1];
        
    }
    else
    {
        [self updateScore:(NSInteger)-1];
        
    }
    [self nextColor];
}

- (IBAction)redButtonPressed
{
    if ([currentColor isEqualToString:@"Red"])
    {
        [self updateScore:(NSInteger)1];
        
    }
    else
    {
        [self updateScore:(NSInteger)-1];
        
    }
    [self nextColor];
}

- (IBAction)yellowButtonPressed
{
    if ([currentColor isEqualToString:@"Yellow"])
    {
        [self updateScore:(NSInteger)1];
        
    }
    else
    {
        [self updateScore:(NSInteger)-1];
        
    }
    [self nextColor];
}
- (IBAction)greenButtonPressed
{
    if ([currentColor isEqualToString:@"Green"])
    {
        [self updateScore:(NSInteger)1];
        
    }
    else
    {
        [self updateScore:(NSInteger)-1];
        
    }
    [self nextColor];
}






@end
